namespace MoreLinq;

using System;
using System.Collections.Generic;

static partial class MoreEnumerable
{
    /// <summary>
    /// Returns a sequence with a range of elements in the source sequence
    /// moved to a new offset.
    /// 将一个原列表的fromIndex开始count个个数的值移动到newIndex位置,返回一个新列表
    /// </summary>
    /// <typeparam name="T">Type of the source sequence.</typeparam>
    /// <param name="source">The source sequence.</param>
    /// <param name="fromIndex">
    /// The zero-based index identifying the first element in the range of
    /// elements to move.</param>
    /// <param name="count">The count of items to move.</param>
    /// <param name="toIndex">
    /// The index where the specified range will be moved.</param>
    /// <returns>
    /// A sequence with the specified range moved to the new position.
    /// </returns>
    /// <remarks>
    /// This operator uses deferred execution and streams its results.
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// var result = Enumerable.Range(0, 6).Move(3, 2, 0);
    /// ]]></code>
    /// The <c>result</c> variable will contain <c>{ 3, 4, 0, 1, 2, 5 }</c>.
    /// </example>
    public static IEnumerable<T> Move<T>(this IEnumerable<T> source, int fromIndex, int count, int toIndex)
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        if (fromIndex < 0) throw new ArgumentOutOfRangeException(nameof(fromIndex), "The source index cannot be negative.");
        if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), "Count cannot be negative.");
        if (toIndex < 0) throw new ArgumentOutOfRangeException(nameof(toIndex), "Target index of range to move cannot be negative.");

        if (toIndex == fromIndex || count == 0)
            return source;

        return toIndex < fromIndex
            ? _(source, toIndex, fromIndex - toIndex, count)
            : _(source, fromIndex, count, toIndex - fromIndex);

        static IEnumerable<T> _(IEnumerable<T> source, int bufferStartIndex, int bufferSize, int bufferYieldIndex)
        {
            var hasMore = true;
            bool MoveNext(IEnumerator<T> e) => hasMore && (hasMore = e.MoveNext());

            using var e = source.GetEnumerator();

            for (var i = 0; i < bufferStartIndex && MoveNext(e); i++)
                yield return e.Current;

            var buffer = new T[bufferSize];
            var length = 0;

            for (; length < bufferSize && MoveNext(e); length++)
                buffer[length] = e.Current;

            for (var i = 0; i < bufferYieldIndex && MoveNext(e); i++)
                yield return e.Current;

            for (var i = 0; i < length; i++)
                yield return buffer[i];

            while (MoveNext(e))
                yield return e.Current;
        }
    }
}
